package com.pkk.cloud.support.oauth2.center.runner.integration.authenticator;


import static com.pkk.components.rpc.common.code.AuthoritiesCode.GET_USER_INFO_ERROR;
import static com.pkk.components.rpc.common.code.AuthoritiesCode.INVALID_USERNAME_OR_PASSWORD;

import com.github.pagehelper.util.StringUtil;
import com.pkk.cloud.support.admin.oauth2.api.admin.AdminApi;
import com.pkk.cloud.support.admin.oauth2.api.common.req.PenguinSysUserNameReq;
import com.pkk.cloud.support.admin.oauth2.api.common.resp.PenguinSysUserInfo;
import com.pkk.cloud.support.oauth2.config.integration.IntegrationAuthentication;
import com.pkk.components.moudle.oauth2.AuthServiceException;
import com.pkk.components.rpc.common.code.CommonCode;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.request.RequestHeader;
import com.pkk.components.rpc.response.CommonResponse;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @author peikunkun<br>
 * @version V1.0<br>
 * @Title: 默认登录处理<br>
 * @Description: <><br>
 * @date 2020/3/18 0018 下午 2:26<br>
 * @URL:[]
 */
@Slf4j
@Component
@Primary
public class UsernamePasswordAuthenticator extends AbstractPreparableIntegrationAuthenticator {

  @Resource
  private AdminApi adminApi;

  /**
   * @Description: 自定义的密码认证操作
   * @Param: integrationAuthentication
   * @return: com.pkk.cloud.support.admin.oauth2.api.common.resp.PenguinSysUser
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 3:46
   */
  @Override
  public PenguinSysUserInfo authenticate(IntegrationAuthentication integrationAuthentication) {
    //@formatter:off
    //根据用户名和客户端获取用户信息
    CommonRequest<PenguinSysUserNameReq> reqCommonRequest = new CommonRequest<>(new RequestHeader(), PenguinSysUserNameReq.builder()
        .username(integrationAuthentication.getUsername()).clientId(integrationAuthentication.getClientId()).build());
    CommonResponse<PenguinSysUserInfo> response = adminApi.loadUserByUsername(reqCommonRequest);

    //响应处理
    if (null == response || !response.getHeader().getCode().equals(CommonCode.SUCCESS.getCode())) {
      throw new AuthServiceException(GET_USER_INFO_ERROR);
    } else if (null == response.getBody()) {
      throw new AuthServiceException(INVALID_USERNAME_OR_PASSWORD);
    }
    //@formatter:on
    return response.getBody();
  }

  /**
   * @Description: 认证前置操作
   * @Param: integrationAuthentication
   * @return: void
   * @Author: peikunkun
   * @Date: 2020/3/18 0018 下午 1:12
   */
  @Override
  public void prepare(IntegrationAuthentication integrationAuthentication) {
  }

  /**
   * @Description: 判断是否支持集成认证类型
   * @Param: integrationAuthentication
   * @return: boolean
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 3:45
   */
  @Override
  public boolean support(IntegrationAuthentication integrationAuthentication) {
    //默认的密码登录不填此参数,如钉钉登录可填此参数
    return StringUtil.isEmpty(integrationAuthentication.getAuthType());
  }
}
